//
//  WDGoutWaitVehicleViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/14.
//

#import "WDGoutWaitVehicleViewController.h"
#import "WDGoutWaitVehicleViewModel.h"
#import "WDGoutWaitVehicleBottomView.h"
#import "WDGoutWaitVehicleTopView.h"

@interface WDGoutWaitVehicleViewController ()
@property (nonatomic, strong) WDGoutWaitVehicleViewModel *viewModel;

@property (nonatomic, strong) WDGoutWaitVehicleTopView *topView;

@property (nonatomic, strong) WDGoutWaitVehicleBottomView *bottomView;
@end

@implementation WDGoutWaitVehicleViewController

- (void)dealloc
{
    debugMethod();
}

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithViewModel {
    _viewModel = [WDGoutWaitVehicleViewModel new];
    _viewModel.serviceModel = self.serviceModel;
    [_viewModel setupData];
    
}


- (void)initWithNavBar {
    [super initWithNavBar];
    [self initWithBackButton];
    
//    [self initWithNavRightButton];
    
    self.navigationItem.title = self.viewModel.title;
}

- (void)initWithSubView {
    [super initWithSubView];
    
    [self initWithRootContent:NO];
    self.rootContent.height = self.rootContent.height - TABBAR_HEIGHT;
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    self.rootContent.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    
    [self initWithTopView];
    [self initWithBottomView];
}

- (void)initWithTopView {
    if (!_topView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kWDGoutWaitVehicleTopViewHeight);
        _topView = [[WDGoutWaitVehicleTopView alloc] initWithFrame:frame];
        _topView.hiddenApplyCancel = ![[EDSAppDelegateTool sharedInstance] hasPermission:@(10010303) parentCode:@(100103)];
        [self.rootContent addSubview:_topView];
        @weakify(self);
        // 申请取消
        _topView.touchApplyCancelBlock = ^{
            @strongify(self);
            [self actionWithApplyCancel];
        };
        _topView.touchCallPhoneBlock = ^{
            @strongify(self);
            [EDSUtility actioinCall:self.viewModel.serviceModel.repairPhone];
        };
        _topView.touchDetailBlock = ^{
            @strongify(self);
            [self actionWithDetail];
        };
    }
    [self.topView updateWithViewData:self.viewModel.serviceModel];
}


- (void)initWithBottomView {
    
    if (![[EDSAppDelegateTool sharedInstance] hasPermission:@(10020202) parentCode:@(100202)] && ![[EDSAppDelegateTool sharedInstance] hasPermission:@(10020203) parentCode:@(100202)]) {
        return;
    }
    
    
    CGRect frame = CGRectMake(0, self.rootContent.bottom, kScreenW, TABBAR_HEIGHT);
    _bottomView = [[WDGoutWaitVehicleBottomView alloc] initWithFrame:frame];
    _bottomView.btnLeftTitle = @"申请取消救援";
    _bottomView.hiddenLeft = ![[EDSAppDelegateTool sharedInstance] hasPermission:@(10020203) parentCode:@(100202)];
    _bottomView.btnRightTitle = @"确认接车";
    _bottomView.hiddenRight = ![[EDSAppDelegateTool sharedInstance] hasPermission:@(10020202) parentCode:@(100202)];
    [self.view addSubview:_bottomView];
        @weakify(self);
    _bottomView.viewBlock = ^(id  _Nullable data) {
        @strongify(self);
        if ([data isEqualToString:@"left"]) { // 申请取消救援
            [self actionWithApplyCancelRescue];
        } else { // 确认接车
            [self actionWithSubmitReceive];
        }
    };
}

/// 申请取消
- (void)actionWithApplyCancel {
    NSMutableDictionary *params = @{@"serviceModel":self.viewModel.serviceModel}.mutableCopy;
    if (self.popResultBlock) {
        params[@"block"] = self.popResultBlock;
    } else {
        @weakify(self);
        void(^block)(id data) = ^(id data) {
            @strongify(self);
            
        };
        params[@"block"] = self.popResultBlock;
    }
    
    UIViewController *vc = [[TYZRouter sharedInstance] getServiceCancelWorkOrderVC:params];
    [TYZJumpVCManager pushBaseVC:self pushVC:vc];
}

/// 工单详情
- (void)actionWithDetail {
    
    if (![[EDSAppDelegateTool sharedInstance] hasPermission:@(10020201) parentCode:@(100202)]) {
        [SVProgressHUD showInfoWithStatus:@"您没有查看详情的权限！"];
        return;
    }
    
    
    NSMutableDictionary *params = @{@"serviceModel":self.viewModel.serviceModel}.mutableCopy;
    @weakify(self);
    void(^block)(id data) = ^(id data) {
        @strongify(self);
//            [self doRefreshData];
    };
    params[@"block"] = block;
    
    UIViewController *vc = [[TYZRouter sharedInstance] getGoutWaitVehicleDetailVC:params];
    [TYZJumpVCManager pushBaseVC:self pushVC:vc];
}

/// 申请取消救援
- (void)actionWithApplyCancelRescue {
    NSMutableDictionary *params = @{@"serviceModel":self.viewModel.serviceModel}.mutableCopy;
    if (self.popResultBlock) {
        params[@"block"] = self.popResultBlock;
    } else {
        @weakify(self);
        void(^block)(id data) = ^(id data) {
            @strongify(self);
            
        };
        params[@"block"] = self.popResultBlock;
    }
    
    UIViewController *vc = [[TYZRouter sharedInstance] getGrabApplyCancelRescueVC:params];
    [TYZJumpVCManager pushBaseVC:self pushVC:vc];
}

/// 确认接车
- (void)actionWithSubmitReceive {
//    任务单[20250413165531-10000061,申请[取消工单],待审核。不可操作该任务单。
    
//    [self.taskDetailModel.applyType integerValue] == 1) {
//                    str = @"已申请取消救援";
//                } else if (self.taskDetailModel.applyType && [self.taskDetailModel.applyType integerValue] == 2) {
//                    str = @"已申请关闭工单";
    
//    return;
    
    @weakify(self);
    void(^block)(id data) = ^(id data) {
        @strongify(self);
        if (self.popResultBlock) {
            self.popResultBlock(nil);
        }
    };
//    NSDictionary *params = @{@"scanTitle": @"扫码接车", @"block":block};
    NSDictionary *params = @{
        @"scanTitle": @"扫码接车",
        @"tipInfo": @"将二维码或条形码对准扫码区域，即可自动扫码扫码配件可以确认配件的版本及适配车辆",
        @"serviceModel": self.viewModel.serviceModel,
        @"block":block
    };
    UIViewController *scanCodeVC = [[TYZRouter sharedInstance] getGoutWaitVehicleScanVC:params];
    [TYZJumpVCManager pushBaseVC:self pushVC:scanCodeVC];
}

@end
